struts2实现简易的登录注册效果

 1 package MyTrain01;
 2 
 3 public class LoginAction {
 4     private String msg ;
 5     private User user;
 6     
 7     public User getUser() {
 8         return user;
 9     }
10     public void setUser(User user) {
11         this.user = user;
12     }
13     public String getMsg() {
14         return msg;
15     }
16     public void setMsg(String msg) {
17         this.msg = msg;
18     }
19     public String login(){
20         if(user.getUsername().equals("admin")&&user.getPassword().equals("admin")){
21             msg = "welcome,"+user.getUsername();
22             return "success";
23         }else{
24             msg = "用户名或密码错误";
25             return "error";
26         }
27     }
28     public String register(){
29         msg = "你好,注册成功";
30         return "success";
31     }
32     
33 }
 1 package MyTrain01;
 2 
 3 public class User {
 4     private String username;
 5     String password;
 6     public String getUsername() {
 7         return username;
 8     }
 9     public void setUsername(String username) {
10         this.username = username;
11     }
12     public String getPassword() {
13         return password;
14     }
15     public void setPassword(String password) {
16         this.password = password;
17     }
18     
19     
20 }
 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 5     "http://struts.apache.org/dtds/struts-2.0.dtd">
 6 <struts>
 7       <package name="test01" namespace="/test01" extends="struts-default">
 8         <action name="login_*" class="MyTrain01.LoginAction" method="{1}">
 9             <result name="success">/User/success.jsp</result>
10             <result name="error">/User/success.jsp</result>
11         </action>
12         
13     </package>
14 </struts>
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 4 <%@ taglib prefix="s" uri="/struts-tags" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12     <s:form action="/test01/login_login.action" method="post" >
13         <table>
14             <tr>
15                 <td>用户名:</td>
16                 <td><input type="text" name="user.username"></td>
17             </tr>
18             <tr>
19                 <td>密码:</td>
20                 <td><input type="password" name="user.password"></td>
21             </tr>
22         </table>
23         <button>登录</button>
24     </s:form>
25 </body>
26 </html>
 1 <%@ page language="java" contentType="text/html; charset=utf-8"%> 
 2 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 3 <%@ taglib prefix="s" uri="/struts-tags" %> 
 4 <!DOCTYPE html PUBLIC "-//W 3C //DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
 5 <html> 
 6     <head> 
 7         <title>Hello</title> 
 8     </head> 
 9     <body> 
10         <h3>${msg }</h3> 
11     </body> 
12 </html> 
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 4 <%@ taglib prefix="s" uri="/struts-tags" %>
 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 6 <html>
 7 <head>
 8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 9 <title>Insert title here</title>
10 </head>
11 <body>
12     <s:form action="/test01/login_register.action" method="post" >
13         <table>
14             <tr>
15                 <td>用户名:</td>
16                 <td><input type="text" name="user.username"></td>
17             </tr>
18             <tr>
19                 <td>密码:</td>
20                 <td><input type="password" name="user.password"></td>
21             </tr>
22         </table>
23         <button>注册</button>
24     </s:form>
25 </body>
26 </html>

 

posted @ 2015-09-01 18:58  北斗星空的笨小孩  阅读(361)  评论(0编辑  收藏  举报