web.xml 配置如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<? 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 id = "WebApp_ID" version = "2.5" > < display-name >Struts2Test</ display-name > <!-- < context-param > < param-name >contextConfigLocation</ param-name > < param-value >/WEB-INF/applicationContext*.xml</ param-value > </ context-param > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > < 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.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" /> <constant name="struts.objectFactory" value="spring" /> 配置字符编码为UTF-8 <constant name="struts.i18n.encoding" value="UTF-8" /> 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 <constant name="struts.serve.static.browserCache" value="false" /> 当struts的配置文件修改后,系统是否自动重新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 <constant name="struts.configuration.xml.reload" value="true" /> <constant name="struts.action.extension" value="do,action,jhtml,," /> <package name="helloworld" namespace="/" extends="struts-default"> <action name="login" class="login"> <result name="success">/second.jsp</result> </action> </package> --> <!-- 国际化编码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 定位视图资源的根路径。默认值为/WEB-INF/content --> <constant value="/" name="struts.convention.result.path" /> <!-- 指定convention扫描以xxx结尾的包 --> <constant value="wpan,com" name="struts.convention.package.locators" /> <!-- 是否将Action类转换成小写 --> <constant value="false" name="struts.convention.package.lowercase" /> <!-- 是否将actionName分割,去掉action部分,以大写字母作为分割 --> <constant value="" name="struts.convention.action.name.separator" /> <!-- 设置默认的父包 --> <constant value="MAIN" name="struts.convention.default.parent.package" /> <package name="MAIN" extends="struts-default" namespace="/"> </package> </struts>
applicationContext.xml 放在WEB-INF目录下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "UTF-8" ?> <!--DOCTYPE < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:context = "http://www.springframework.org/schema/context" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" xmlns:jee = "http://www.springframework.org/schema/jee" xmlns:jaxws = "http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/jee
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/tx
http://cxf.apache.org/jaxws <!-- < context:component-scan base-package = "*" /> </ beans > |
struts2配置 放在scr目录下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<? 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 id = "WebApp_ID" version = "2.5" > < display-name >Struts2Test</ display-name > <!-- < context-param > < param-name >contextConfigLocation</ param-name > < param-value >/WEB-INF/applicationContext*.xml</ param-value > </ context-param > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener > < 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.jsp</ welcome-file > </ welcome-file-list > </ web-app > |
action写法 目录:src.com.action
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package com.action; import org.apache.struts2.convention.annotation.Action; import org.apache.struts2.convention.annotation.ExceptionMapping; import org.apache.struts2.convention.annotation.ExceptionMappings; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import org.apache.struts2.convention.annotation.ParentPackage; import org.springframework.beans.factory.annotation.Autowired; import com.opensymphony.xwork2.ActionSupport; import com.service.PersonService; @Namespace ( "/person" ) //访问路径的包名 @Results ( @Result (name "success" , "/index.jsp" ), @Result (name "error" , "/hello.jsp" ) @ExceptionMappings ( @ExceptionMapping (exception "java.lange.RuntimeException" , "error" ) public class
extends ActionSupport @Autowired private transient
public PersonAction() // } @Action ( "login" ) //访问路径的action名, public String System.out.println( "login-index" ); personService.selPerson(); return SUCCESS; } } |
dao 写法 目录:src.com.dao.impl 接口在 src.com.dao 此处略
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
package com.dao.impl; import org.springframework.stereotype.Repository; import com.dao.PersonDao; @Repository ( "personDao" ) public class
implements PersonDao public PersonDaoImpl() // } @Override public void
// System.out.println( "运行dao" ); } } |
service 写法 目录:src.com.service.impl,接口在 src.com.service 此处略
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
package com.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.dao.PersonDao; import com.service.PersonService; @Service ( "personService" ) public class
implements PersonService @Autowired private transient
//注入的DAO public PersonServiceImpl() // } @Override public void
// System.out.println( "运行service" ); personDao.selPerson(); } }
|