今天终于配置好了ssh框架的整合,记录下过程供参考
环境:window8.1,jdk1.7 ,带有javaee的eclipse,也就是说要能发布web项目,TOMCAT服务器,tomcat配置涉及到环境变量,以及在eclipse中添加tomcat不在多述,struts2.1.8,hibernate3.3.2,spring2.5.6
总体结构:
lib下的jar包有几个不是必要的,但随着项目的发展估计要用到
1.配置struts2
先导入jar包
这个推荐去apps的目录下中的WEB-INF的lib中直接复制即可,在项目lib中右键paste
然后依次配置web.xml和struts.xml,值得注意的是,我的eclipse版本,在新建项目时,要点next到最后一步,单选框为General的要打勾,不然不会出现web.xml,至于后来自己添加web.xml是否可行,我并没有试过
web.xml可以直接去struts包中的例子去找,加入filter,filterMapping即可,这里贴出代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>oa</display-name> <filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter> <filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
配置struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd"> <struts>
<constant name="struts.devMode" value="true" /> <package name="id" namespace="/test" extends="struts-default">
<action name="helloworld" class="com.beans.HelloWorldAction" method="execute">
<result name="success">/WEB-INF/page/hello.jsp</result>
</action>
<action name="as">
<result type="redirect">/ex.jsp</result>
</action> </package> <!-- Add packages here --> </struts>
<constant name="struts.devMode" value="true" />为开发者模式,即修改xml配置文件无需重新部署项目即可生效
第二个action是重定向,可无视
在WEB-INF/page目录下建立一个hello.jsp,随意书写
在src中创建包com.beans,创建HelloWorldAction类
package com.beans; import java.util.Date;
public class HelloWorldAction {
private String msg;
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
} public String execute()
{
return "success";
}
}
在webcontent加入index.jsp
至此,struts2配置完成,右键单击项目名,run as server,会显示index.jsp的页面
在浏览器中输入localhost:8080/项目名/test/helloworld
/test 对应的struts.xml中的包路径 helloworld
就会显示hello.jsp的页面,struts配置成功
配置struts中容易出现几个问题
1.action or result can not found
出现这一类问题大多是书写上的问题,或者直接就没写,action,result,返回字符串是否有空格..,多检查下即可
2.错误导入jar包,这是一个曾经困扰了我很长时间的问题,我看的第一本书,直接让我把lib下的全部jar包导入进去,那简直就是地狱,无穷无尽的错误
整个ssh的配置过程,一共花了6个小时,把这些记录下来估计也要这个时间,我会慢慢更新
因为struts之后还要与spring做一次整合,有些文件内容已经改了,改过的部分我已经还原了,但说不准又会衍生出新的问题,欢迎指正