1,建立javaee5的web project;在lib包中放入以下几个包
形成的工程结构图如下所示:
2,创建视图页面login.jsp success.jsp error.jsp
其中login.jsp的代码如下:
View Code
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="struts" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <center> <h2>用户登录</h2> <struts:form action="login" method="post"> <struts:textfield name="userName" label="帐号:"></struts:textfield> <struts:password name="userPsw" label="密码:"></struts:password> <struts:submit value="登录"></struts:submit> </struts:form> </center> </body> </html>
success.jsp的代码如下:
View Code
<body> <center><h2>欢迎登录</h2></center><br/> 当前登录用户:<struts:property value="userName"/> </body>
error.jsp的代码如下:
View Code
<body> 登录失败 </body>
3,创建Action类LoginAction.java
View Code
package action;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
private String userName;
private String userPsw;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPsw() {
return userPsw;
}
public void setUserPsw(String userPsw) {
this.userPsw = userPsw;
}
/**
* @return
*/
public String execute() {
if(getUserName().equals("123")&& getUserPsw().equals("123")){
return SUCCESS;
}
else return ERROR;
}
}
4,为解决中文乱码问题,创建过滤器:CharacterEncodingFilter.java
View Code
package filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class CharacterEncodingFilter implements Filter {
private String characterEncoding;//编码方式在web.xml中
private boolean enabled;//是否启动filter,初始值在web.xml中
@Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
//初始化时加载参数
//从配置文件中读取设置到编码方式
characterEncoding=config.getInitParameter("characterEncoding");
//启动该过滤器完成编码方式到修改
enabled="true".equalsIgnoreCase(characterEncoding.trim())
||"1".equalsIgnoreCase(characterEncoding.trim());
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
if(enabled||characterEncoding!=null){
request.setCharacterEncoding(characterEncoding);//设置request编码方式
response.setCharacterEncoding(characterEncoding);//设置response编码方式
}
chain.doFilter(request, response);//doFilter将修改好的request和response参数向下传递;
}
@Override
public void destroy() {
// TODO Auto-generated method stub
characterEncoding=null;
}
}
5,配置web.xml
View Code
<?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>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>struts.action.extension</param-name> <param-value>action</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>characterEncoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>enable</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
6,创建资源信息文件struts.properties
View Code
struts.action.extension=action
7,配置struts.xml文件
View Code
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="main" extends="struts-default"> <action name="login" class="action.LoginAction"> <result name="error">/error.jsp</result> <result name="success">/success.jsp</result> </action> </package> </struts>
8,发布即可