现在的位置: 首页 > 综合 > 正文

spring与struts整合注解版

2019年05月21日 ⁄ 综合 ⁄ 共 7491字 ⁄ 字号 评论关闭

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
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns: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_2_5.xsd"
    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
<?xmlversion="1.0"encoding="UTF-8"?>
<!--DOCTYPE
beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"    "http://www.springframework.org/dtd/spring-beans-2.0.dtd"-->
<beansxmlns="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/jee/spring-jee.xsd

    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/beans/spring-beans-3.0.xsd

    http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

    http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

    http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd ">
    
 
  
 <!--
使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入,也可以分开注释,或者固定某个目录下 -->
 <context:component-scanbase-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
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appxmlns: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_2_5.xsd"
    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
packagecom.action;
 
importorg.apache.struts2.convention.annotation.Action;
importorg.apache.struts2.convention.annotation.ExceptionMapping;  
importorg.apache.struts2.convention.annotation.ExceptionMappings;
importorg.apache.struts2.convention.annotation.Namespace;
importorg.apache.struts2.convention.annotation.Result;
importorg.apache.struts2.convention.annotation.Results;
importorg.apache.struts2.convention.annotation.ParentPackage;
importorg.springframework.beans.factory.annotation.Autowired;
 
importcom.opensymphony.xwork2.ActionSupport;
importcom.service.PersonService;
 
@Namespace("/person"//访问路径的包名
@Results(
@Result(name
"success",
location = 
"/index.jsp"),
        @Result(name
"error",
location = 
"/hello.jsp")
})
@ExceptionMappings(
@ExceptionMapping(exception
"java.lange.RuntimeException",
result = 
"error")
})
publicclass

PersonAction 
extendsActionSupport
{
 
    @Autowired
    privatetransient

PersonService personService;
    publicPersonAction()
{
        //
TODO Auto-generated constructor stub
    }
    @Action("login")  //访问路径的action名,
想要访问selUser 这个方法 地址为 http://localhost:8080/工程名/yang/login
     publicString
selUser(){
            System.out.println("login-index");
            personService.selPerson();
         returnSUCCESS;
         }
}



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
packagecom.dao.impl;
 
importorg.springframework.stereotype.Repository;
 
importcom.dao.PersonDao;
@Repository("personDao"
publicclass

PersonDaoImpl 
implementsPersonDao
{
 
    publicPersonDaoImpl()
{
        //
TODO Auto-generated constructor stub
    }
 
    @Override
    publicvoid

selPerson() {
        //
TODO Auto-generated method stub
        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
packagecom.service.impl;
 
 
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
 
 
importcom.dao.PersonDao;
importcom.service.PersonService;
@Service("personService")
publicclass

PersonServiceImpl 
implementsPersonService
{
 
 
@Autowired
privatetransient

PersonDao personDao   ;
//注入的DAO
 
publicPersonServiceImpl()
{
//
TODO Auto-generated constructor stub
}
 
 
@Override
publicvoid

selPerson() {
//
TODO Auto-generated method stub
System.out.println("运行service");
personDao.selPerson();
}
}

抱歉!评论已关闭.