关于结果处理类型input的代码演示-深入Struts2
程序员文章站
2022-07-02 12:10:10
...
一 Input因为年龄输入错误自动跳转回输入界面
1、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>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/error.jsp</result>
</action>
<action name="*_*_*" method="{2}" class="com.cakin.{3}.{1}Action">
<result>/result.jsp</result>
<result name="add">/{2}.jsp</result>
<result name="update">/{2}.jsp</result>
</action>
<action name="LoginAction" method="login" class="com.cakin.action.LoginAction">
<result>/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
2、User.java
package com.cakin.po;
import java.util.List;
public class User {
private String username;
private String password;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
private List<User> booklist;
public List<User> getBooklist() {
return booklist;
}
public void setBooklist(List<User> booklist) {
this.booklist = booklist;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
3、login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="LoginAction.action" method="post">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
书籍1:<input type="text" name="booklist[0].username">
书籍2:<input type="text" name="booklist[1].username">
年龄:<input type="text" name="age">
<input type="submit" value="提交" />
</form>
</body>
</html>
4、测试
二 通过addFieldError跳回到输入页码,并带错误提示
1、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>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/error.jsp</result>
</action>
<action name="*_*_*" method="{2}" class="com.cakin.{3}.{1}Action">
<result>/result.jsp</result>
<result name="add">/{2}.jsp</result>
<result name="update">/{2}.jsp</result>
</action>
<action name="LoginAction" method="login" class="com.cakin.action.LoginAction">
<result>/success.jsp</result>
<result name="input">/login.jsp</result>
</action>
</package>
</struts>
2、login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form action="LoginAction.action" method="post">
用户名:<input type="text" name="username"><s:fielderror name="username"></s:fielderror>
密码:<input type="password" name="password">
书籍1:<input type="text" name="booklist[0].username">
书籍2:<input type="text" name="booklist[1].username">
年龄:<input type="text" name="age">
<input type="submit" value="提交" />
</form>
</body>
</html>
3、LoginAction
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private User user =new User();
public String login(){
if(user.getUsername()==null
||"".equals(user.getUsername()))
{
this.addFieldError("username", "用户名为空");
return INPUT;
}
System.out.println(user.getUsername());
System.out.println(user.getBooklist().get(0).getUsername());
System.out.println(user.getBooklist().get(1).getUsername());
return SUCCESS;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
4、测试
三 通过validate方法实现调回到输入页码
1、LoginAction.jsp
package com.cakin.action;
import com.cakin.po.User;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class LoginAction extends ActionSupport implements ModelDriven<User>{
private User user =new User();
public String login(){
System.out.println(user.getUsername());
System.out.println(user.getBooklist().get(0).getUsername());
System.out.println(user.getBooklist().get(1).getUsername());
return SUCCESS;
}
@Override
public void validate() {
// TODO Auto-generated method stub
if(user.getUsername()==null
||"".equals(user.getUsername()))
{
this.addFieldError("username", "用户名为空");
//return INPUT;
}
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
2、测试