package action; import com.opensymphony.xwork2.ActionSupport; import entity.User; public class UserAction extends ActionSupport { private User user = new User(); public String login() { System.out.println(user.getUserName() + "\n" + user.getPassword()); return "success"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
package action; import org.apache.struts2.convention.annotation.Namespace; import org.apache.struts2.convention.annotation.Result; import org.apache.struts2.convention.annotation.Results; import com.opensymphony.xwork2.ActionSupport; import entity.User; @Namespace("/user") @Results( { @Result(name="success",location="/success.jsp"), @Result(name="fail",location="/fail.jsp") } ) public class User2Action extends ActionSupport { private User user = new User(); public String login() { System.out.println(user.getUserName() + "\n" + user.getPassword()); return "success"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } }
<?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> <constant name="struts.i18n.encoding" value="utf-8"></constant> <!-- 字符编码 --> <!--<constant name="struts.devMode" value="true"></constant> 开发模式 --> <!--<constant name="struts.action.extension" value="action"></constant> Action后缀 --> <!--<include file=""></include> 配置文件 --> <!--<bean class=""></bean> 配置Bean --> <package name="user" namespace="/user" extends="struts-default"> <!--<interceptors> --><!-- 拦截器配置 --> <!--<interceptor name="" class=""></interceptor> 配置拦截器 --> <!--</interceptors> --><!--<global-results> 全局resulst --> <!--<result name=""></result> 配置result --> <!--</global-results> --><!--<global-exception-mappings> 全局异常 --> <!--<exception-mapping result="" exception=""></exception-mapping> 配置异常 --> <!--</global-exception-mappings> --><action name="userAction" class="action.UserAction"> <result name="success">/success.jsp</result> </action> </package> </struts>
<%@ 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 'index.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="user/userAction!login" method="post"> <input type="text" name="user.userName" /><br/> <input type="password" name="user.password"><br/> <input type="submit" value="登录" /> </form> </body> </html>
<%@ 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 'success.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> ${user.userName }登录成功... </body> </html>