首页新闻找找看学习计划

用struts2怎么做?

0
[已解决问题] 解决于 2016-05-05 15:36

我是没接触过这个东西的,昨天跟今天有看,然后现在这样子我不知道到底是什么情况,到底这一部分做完没,接下来该怎么做,愿意说下的就说下呗

 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 <struts>
 6     <package name="default" extends="struts-default">
 7         <action name="log" class="per.zxl.TestAction" method="msg">
 8             <result>/login.jsp</result>
 9         </action>
10     </package>
11 </struts>
struts配置
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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">
 3   <display-name>struts</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.jsp</welcome-file>
 6   </welcome-file-list>
 7   <filter>
 8       <filter-name>struts2</filter-name>
 9       <filter-class>
10           org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
11       </filter-class>
12   </filter>
13   <filter-mapping>
14       <filter-name>struts2</filter-name>
15       <url-pattern>/*</url-pattern>
16   </filter-mapping>
17 </web-app>
web配置
 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     <form action="log" method="post">
11         <table align="center">
12             <tr>
13                 <td>账号:</td>
14                 <td><input type="text" name="username"></td>
15             </tr>
16             <tr>
17                 <td>密码:</td>
18                 <td><input type="text" name="username"></td>
19             </tr>
20             <tr>
21                 <td>
22                     <button type="submit" value="mitbit">登录</button>
23                 </td>
24             </tr>
25         </table>
26     </form>
27 </body>
28 </html>
index.jsp
 1 package per.zxl;
 2 
 3 import com.opensymphony.xwork2.ActionSupport;
 4 
 5 public class TestAction extends ActionSupport{
 6     
 7     private String username;
 8     private String password;
 9     //get set
10     public String getUsername() {
11         return username;
12     }
13     public void setUsername(String username) {
14         this.username = username;
15     }
16     public String getPassword() {
17         return password;
18     }
19     public void setPassword(String password) {
20         this.password = password;
21     }
22     
23     
24 
25     public String msg(){
26         return SUCCESS;
27     }
28     
29     
30 }
TestAction
 1 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 2     pageEncoding="ISO-8859-1"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10     is login
11 </body>
12 </html>
login.jsp

其中,login.jsp我先前用来测试的,现在想做的是,当输入用户名或者密码后,判断它是否正确,正确就跳转到login.jsp,错误就跳转到新建页面error.jsp中,但我现在不晓得该怎么做了

问题补充:

顺带问下,如果加上jdbc,该怎么做?

cocoyoona的主页 cocoyoona | 初学一级 | 园豆:20
提问于:2016-05-05 14:41
< >
分享
最佳答案
1

http://www.cnblogs.com/xing901022/p/3961661.html

根据方法里面的return返回值,这是例子

奖励园豆:5
小光 | 小虾三级 |园豆:1830 | 2016-05-05 14:51
其他回答(1)
1

struts.xml中result 可以配置多个的:

<result name="success">/login</result>
<result name="error">/error.jsp</result>

在msg方法中判断账号和密码,正确返回"success",错误返回"error"

萧萧弈寒 | 园豆:168 (初学一级) | 2016-05-05 14:50

是这样么.判断实体的username.equals(放表单中的username)?

支持(0) 反对(0) cocoyoona | 园豆:20 (初学一级) | 2016-05-05 14:52
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册