![struts2 Demo struts2 Demo](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
参考资料 :
http://www.cnblogs.com/yangy608/archive/2010/11/08/1871962.html
http://www.yiibai.com/struts2/struts_quick_guide.html
个人理解:
首先我们要清楚struts是一种MVC框架、
strut1和struts2有所区别。最明显的应该是Struts2把Struts1的actionForm和action的结合使用,并且优化了execute方法,通过String就可以实现跳转文件。
struts.xml
该配置文件的作用:配置action。
action配置:
name action名
class 类路径
result:action result 页面跳转配置
(个人理解:这里比struts1有所简化,省去了form的配置以及action和form 的关联配置.)
以下为struts2的Demo工程源码:
1.建立web工程,引入struts2包
2.建立package:action包
2.1在action下建立LoginAction.java
package action; import com.opensymphony.xwork2.ActionSupport; public class LoginAction extends ActionSupport { /**
*
*/
private static final long serialVersionUID = 1L; /*
* (non-Javadoc)
*
* @see com.opensymphony.xwork2.ActionSupport#execute()
*/
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
System.out.println("LoginAction.execute()");
System.out.println("name = " + name);
if (name.equals("xiaoming")) {
return "success";
} else {
return "error";
}
} private String name; /**
* @return the name
*/
public String getName() {
return name;
} /**
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
} }
3.配置文件
3.1在scr文件夹下建立struts.xml
(这里的package 的extends一直显示undefined,故引入struts-default.xml文件,result文件也要记得配置好)
<之前一直报错:警告: Could not find action or result
No result defined for action action.LoginAction and result error
解决办法是在package加上 namespace="/"
>
<?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>
<include file="struts-default.xml"></include>
<package name="struts2" namespace="/" extends="struts-default">
<action name="login" class="action.LoginAction">
<result name="success">/WEB-INF/wel.jsp</result>
<result name="error">/WEB-INF/error.jsp</result></action>
</package>
</struts>
3.2web.xml:
(注意 filter-class文件,应该是说struts2.1版本以上的使用如下文件,struts2.0的是另一个文件)
<?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">
<display-name></display-name>
<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>*.action</url-pattern>
</filter-mapping></web-app>
4.编写jsp页面:
4.1 编写input-source 页面index.jsp
<form action="login.action">
name:<input type="text" name="name"> <input type="submit"
value="submit">
</form>
4.2 编写forward页面 error.jsp 和 wel.jsp
<body>
This is wel JSP page. <br>
hello <%=request.getParameter("name") %>
</body>