Struts2+Spring3.0简单示例
本文仅为个人练习的一个简单总结,后续将完成SSH等其它框架的练习。
开发环境:JDK1.6、MyEclipse8.5、Struts2.1、Spring3.0
一、建立WEB工程。例如:sstest3。
二、加入Struts2.1框架。
引用Struts 2 Core Libraries、Struts 2 Spring Libraries类库。
三、加入Spring框架。
引用Spring 3.0 Core Liberaies、Spring 3.0 Web Libraries类库
四、建立前台页面:
1、sstest3\WebRoot\index.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<body>
<s:form action="loginAction">
<s:textfield name="u_name">name:</s:textfield>
<s:submit></s:submit>
</s:form>
</body>
</html>
2、sstest3\WebRoot\main.jsp,内容如下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<s:property value="u_name" />
</body>
</html>
五、修改文件sstest3\src\struts.xml,内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<package name="default" extends="struts-default">
<action name="loginAction" class="login">
<param name="u_name">张三</param>
<result>/main.jsp</result>
<result name="err">/err.jsp</result>
</action>
</package>
</struts>
六、修改文件sstest3\src\applicationContext.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" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="login" class="com.login.LoginAction" />
</beans>
七、新建类文件sstest3\src\com\login\LoginAction.java,内容如下:
package com.login;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String u_name;
public String getU_name() {
return u_name;
}
public void setU_name(String uName) {
u_name = uName;
}
@Override
public String execute() throws Exception {
super.execute();
System.out.println(getU_name());
return SUCCESS;
}
}
八、修改文件sstest3\WebRoot\WEB-INF\web.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
</web-app>
九、发布、测试。
http://localhost:8080/sstest3/
通过。