Example of using automatic assembly operation for the integration of spring and struts
•
Java
This paper describes the integration of spring and struts using automatic assembly operations. Share with you for your reference, as follows:
I. web configuration
<?xml version="1.0" encoding="GBK"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 使用ContextLoaderListener初始化spring容器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 定义Struts 2的FilterDispathcer的Filter --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 --> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
II. ApplicationContext XML configuration
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <!-- 定义一个业务逻辑组件,实现类为MyServiceImp, 此处的id必须与Action的setter方法名对应 --> <bean id="ms" class="org.crazyit.app.service.impl.MyServiceImpl"/> </beans>
Three views
1 loginForm. jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>登录页面</title> </head> <body> <h3>用户登录</h3> <s:form action="login"> <s:textfield name="username" label="用户名"/> <s:textfield name="password" label="密码"/> <tr align="center"> <td colspan="2"> <s:submit value="登录" theme="simple"/> <s:reset value="重设" theme="simple"/> </td> </tr> </s:form> </body> </html>
2 welcome. jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <%@taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>成功页面</title> </head> <body> 您已经登录!<br/> <s:actionmessage /> </body> </html>
3 error. jsp
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>错误页面</title> </head> <body> 您不能登录! </body> </html>
IV. struts configuration
<?xml version="1.0" encoding="GBK"?> <!-- 指定Struts 2配置文件的DTD信息 --> <!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.i18n.encoding" value="GBK"/> <constant name="struts.devMode" value="true"/> <constant name="struts.enable.DynamicMethodInvocation" value="false"/> <package name="lee" extends="struts-default"> <!-- 定义处理用户请求的Action --> <action name="login" class="org.crazyit.app.action.LoginAction"> <!-- 为两个逻辑视图配置视图页面 --> <result name="error">/WEB-INF/content/error.jsp</result> <result>/WEB-INF/content/welcome.jsp</result> </action> <!-- 让用户直接访问该应用时列出所有视图页面 --> <action name="*"> <result>/WEB-INF/content/{1}.jsp</result> </action> </package> </struts>
Five actions
package org.crazyit.app.action; import com.opensymphony.xwork2.ActionSupport; import org.crazyit.app.service.*; public class LoginAction extends ActionSupport { // 下面是用于封装用户请求参数的两个成员变量 private String username; private String password; // 系统所用的业务逻辑组件 private MyService ms; // 设值注入业务逻辑组件所必需的setter方法 public void setMs(MyService ms) { this.ms = ms; } // username的setter和getter方法 public void setUsername(String username) { this.username = username; } public String getUsername() { return this.username; } // password的setter和getter方法 public void setPassword(String password) { this.password = password; } public String getpassword() { return this.password; } // 处理用户请求的execute方法 public String execute() throws Exception { // 调用业务逻辑组件的validLogin()方法 // 验证用户输入的用户名和密码是否正确 if (ms.validLogin(getUsername(),getpassword()) > 0) { addActionMessage("哈哈,整合成功!"); return SUCCESS; } return ERROR; } }
Vi. service
1 interface
package org.crazyit.app.service; public interface MyService { int validLogin(String username,String pass); }
2 implementation class
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class MyServiceImpl implements MyService { public int validLogin(String username,String pass) { // 此处只是简单示范,故直接判断用户名、密码是否符合要求 if ( username.equals("crazyit.org") && pass.equals("leegang") ) { return 99; } return -1; } }
Seven tests
@H_ 502_ 60@
Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills
I hope this article will be helpful to you in Java programming.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码