spring 3統合struts 2例
注:springの役割は実際的にbeanを溶かして、strutsを統合することは実際にspringを使ってaction類を管理する過程です.
1、jarパッケージの導入
strutsとspringのjarを導入して、strutsはもう一つのstruts 2-spring-plugn-23.4.1.jarが必要です.
2、web.xml
strutsとspringの配置が必要です.
actionのクラスはbean類そのものではなく、springのbeanのidの名前です.
中の対象属性はspringが実務化を担当しています.
6、appication Contect.xml
7、login.jsp
なお、actionは、完全なアクセス経路が必要であるというリダイレクトを実現しており、ここでは >>を使用して、完全なパスを取得しています.
[html
view plin
copy
<%@pagelangage="java"contentType="text/html、charset=UTF-8"pagenenccoding="UTF-8"><%@tagliburi="/struts-tags"prefix="s"><%Stringpath=request.get ContactextPath()StringbasePath=request.get Scheme()+「/」+request.get ServerName()+「:」+request.get ServerPort()+path+「/」%><html <basehref=「%=basePath%」><metahttp-equiv=“Content-Type”content=“text/html;charset=UTF-8”<title>Inserttitlehere<body>
1、jarパッケージの導入
strutsとspringのjarを導入して、strutsはもう一つのstruts 2-spring-plugn-23.4.1.jarが必要です.
2、web.xml
strutsとspringの配置が必要です.
<?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>spring_struts2</display-name>
<!-- spring , web , spring classpath: -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- Spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- sturts2 -->
<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>
3、struts.xmlactionのクラスはbean類そのものではなく、springのbeanのidの名前です.
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="ru" namespace="/" extends="struts-default">
<default-action-ref name="index" />
<global-results>
<result name="error">/error.jsp</result>
</global-results>
<action name="login" class="loginaction">
<result name="sucess">
/jsp/logsucess.jsp
</result>
<result name="error">
/jsp/logerror.jsp
</result>
</action>
</package>
</struts>
4、LoginnAction.java(action類)中の対象属性はspringが実務化を担当しています.
package com.ru.action;
import com.opensymphony.xwork2.ActionSupport;
import com.ru.service.UserCheck;
public class LoginAction extends ActionSupport {
private String username;
private String password;
private UserCheck uc;
public void setUc(UserCheck uc) {
this.uc = uc;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String execute() throws Exception {
if(uc.result(username, password)=="sucess"){
return "sucess";
}else{
return "error";
}
}
}
5、UserCheck.java(これは業務ロジック層の判断コードです)package com.ru.service;
public class UserCheck {
public String result(String username,String password){
if("ru".equals(username)&&"123".equals(password)){
return "sucess";
}else{
return "error";
}
}
}
6、appication Contect.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- services -->
<!-- bean -->
<bean id="usercheck" class="com.ru.service.UserCheck"/>
<!-- action -->
<bean id="loginaction" class="com.ru.action.LoginAction">
<property name="uc" ref="usercheck"/>
</bean>
</beans>
7、login.jsp
なお、actionは、完全なアクセス経路が必要であるというリダイレクトを実現しており、ここでは
[html
view plin
copy
<%@pagelangage="java"contentType="text/html、charset=UTF-8"pagenenccoding="UTF-8"><%@tagliburi="/struts-tags"prefix="s"><%Stringpath=request.get ContactextPath()StringbasePath=request.get Scheme()+「/」+request.get ServerName()+「:」+request.get ServerPort()+path+「/」%><html <basehref=「%=basePath%」><metahttp-equiv=“Content-Type”content=“text/html;charset=UTF-8”<title>Inserttitlehere<body>