java struts 2搭建
1.web工程
2.将struts2 用到的jar包,拷贝到webcontent/webinf/lib文件夹。下
3.webcontent 下的web.xml 下配置:
<?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" 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>struts2</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.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>
</web-app>
4.java Resources/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>
//namespase——>命名空间
//extends="struts-default"----->默认继承
//<interceptors> ---->过滤器
//class="cn.stu.strust.MyTime" ----->过滤器所在的包
<package namespace="/" name="cn.stu.strust" extends="struts-default">
<interceptors>
<interceptor name="Mytime" class="cn.stu.strust.MyTime"></interceptor>
</interceptors>
//映射相应
/calss="cn.stu.strust"//action --->所在包类位置
<action name="login" class="cn.stu.strust.loginaction">
<interceptor-ref name="Mytime"></interceptor-ref>//加载自定义过滤器
<interceptor-ref name="defaultStack"></interceptor-ref>//加载默认过滤器
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
<result name="input">index.jsp</result>//验证返回
</action>
</package>
</struts>
5.包结构如下:
6.logginAction.java
package cn.edu.tsu.se.sturts;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;
import cn.edu.tsu.se.sturts.entity.User;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport implements SessionAware{
private User user;
Map<String,Object> session ;
public String login() throws Exception {
// TODO Auto-generated method stubyste
System.out.println("action doing");
if(user.getN1().equals( user.getPwd())){
//Map<String,Object> session = ActionContext.getContext().getSession();
session.put("name", user.getN1());
// HttpSession session = ServletActionContext.getRequest().getSession();
//session.setAttribute("name", user.getN1());
return "success";
}else{
return "error";
}
}
@Override
public void validate() {
// TODO Auto-generated method stub
if(user.getPwd().equals("1111")){
this.addFieldError("user.pwd", "密码太简单");
}
}
public void setUser(User user) {
this.user = user;
}
public User getUser(){
return user;
}
@Override
public void setSession(Map<String, Object> arg0) {
// TODO Auto-generated method stub
session = arg0;
}
}