最简单的Struts2登录

web.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app version="2.5" 
 3     xmlns="http://java.sun.com/xml/ns/javaee" 
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 5     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 6     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 7   <welcome-file-list>
 8     <welcome-file>index.jsp</welcome-file>
 9   </welcome-file-list>
10   
11   <filter>
12       <filter-name>struts2</filter-name>
13       <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
14   </filter>
15   
16   <filter-mapping>
17       <filter-name>struts2</filter-name>
18       <url-pattern>/*</url-pattern>
19   </filter-mapping>
20   
21 </web-app>
View Code

index.jsp:

 1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 <form action="login" method="post">
 9 姓名:<input type="text" name="username"><br/>
10 密码:<input type="text" name="password"><br/>
11 <input type="submit" value="提交">
12 </form>
13 </body>
14 </html>
View Code

struts.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4     "http://struts.apache.org/dtds/struts-2.0.dtd">
 5 
 6 <struts>
 7 
 8     <package name="default" extends="struts-default">
 9         <action name="login" class="com.action.LoginAction">
10            <result name="success">/success.jsp</result>
11            <result name="input">/index.jsp</result>
12         </action>
13     </package>
14 
15 </struts>
View Code

LoginAction.java:

 1 import org.apache.struts2.ServletActionContext;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 /**
 6  *@date 2014-4-17
 7  */
 8 public class LoginAction extends ActionSupport {
 9 
10     private String username;
11     private String password;
12     public String getUsername() {
13         return username;
14     }
15     public void setUsername(String username) {
16         this.username = username;
17     }
18     public String getPassword() {
19         return password;
20     }
21     public void setPassword(String password) {
22         this.password = password;
23     }
24     @Override
25     public String execute() throws Exception {
26         System.out.println("hello");
27         if(username.equals("scott")&& password.equals("tiger")){
28             ServletActionContext.getRequest().getSession().setAttribute("username",username);
29             return "success";
30         }else{
31             return "input";
32         }
33     }
34     
35 }
View Code

success.jsp:

 1 <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 2 <html>
 3 <head>
 4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 5 <title>Insert title here</title>
 6 </head>
 7 <body>
 8 欢迎${username}登录系统...
 9 </body>
10 </html>
View Code

注意:

struts.xml 文件可以到struts2开发包,我这里是2.1.8.1下的apps目录下寻找,将struts2-blank-2.1.8.1.war后缀改为rar,然后解压,WEB-INF--->src--->java--->struts.xml。

posted on 2014-04-17 11:28  confirmBname  阅读(203)  评论(0编辑  收藏  举报

导航