刚开始学习struts2 从第一步环境配置开始 记录学习
1、从官网下载struts-2.5.12-src 、struts-2.5.12-all 在all/lib中有所需要的jar包。
2、新建一个动态的web工程Struts2Tes
(1)配置web.xml 文件,在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">
<display-name></display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>test.jsp</welcome-file>
</welcome-file-list>
</web-app>
其中,
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
相比较2.3有了变化,要注意!
(2)导入jar包 放到WEB-INF/lib 中
相比较2.3版本的struts jar包有了变化 其中2.3的xwork-core 在2.5中并入到了struts2-core中。
(3)写一个简单的登录页面 test.jsp
<body>
<form action="check" method="post">
登录<br />
账号:<input type="text" name="username" /><br />
密码:<input type="password" name="password" /><br />
<input type="submit" value="登录" />
</form>
</body>
(4)显示处理的Java类 Test.java
package com.hanxy.struts;
import com.opensymphony.xwork2.ActionSupport;
public class Test extends ActionSupport{
private String username;
private String userpasswd;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpasswd() {
return userpasswd;
}
public void setUserpasswd(String userpasswd) {
this.userpasswd = userpasswd;
}
public String execute(){
if(username.equals("name")){
return "success";
}else{
return "error";
}
}
}
(5)配置struts.xml文件
在src创建一个struts.xml文件
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="p1" extends="struts-default" namespace="/" strict-method-invocation="false">
<action name="check" class="com.hanxy.struts.Test">
<result name="success">success.jsp</result>
<result name="error">error.jsp</result>
</action>
</package>
</struts>
(6)新建success.jsp error.jsp 内容自己写写得了