通过案例说明struts2的工作流程
本文主要是通过一个例子来说明Struts2的一个工作流程。
首先定义一个登录页面login.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>登录页面</title>
- </head>
- <body>
- <s:form action="loginUser">
- <s:label value="登录页面"></s:label>
- <s:textfield name="user.userName" label="用户名:"></s:textfield>
- <s:textfield name="user.name" label="姓名:"></s:textfield>
- <s:password name="user.password" label="登录密码:"></s:password>
- <s:submit value="登录"></s:submit>
- </s:form>
- </body>
- </html>
定义一个登录成功跳转到欢迎页面welcome.jsp:
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ taglib prefix="s" uri="/struts-tags" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>欢迎页面</title>
- </head>
- <body>
- 欢迎<FONT color="red" style="font-weight: bold" >"<s:property value="user.name"/>"</FONT>登录系统
- </body>
- </html>
定义一个用户实体类User.java:
- package org.ml.bean;
- /**
- * 用户实体类
- *
- * @author MuLing
- *
- */
- public class User {
- private String userName;// 用户名
- private String name;// 用户姓名
- private String password;// 用户密码
- 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;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- }
定义一个Action:UserAction.java
- package org.ml.action;
- import org.ml.bean.User;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 用户实体对于的Action
- *
- * @author MuLing
- *
- */
- public class UserAction extends ActionSupport {
- private static final long serialVersionUID = 1L;
- private User user ;
- /**
- * 自定义login方法
- *
- * @return
- */
- public String login() {
- if ("admin".equals(user.getUserName()) && "admin".equals(user.getPassword())) {
- return SUCCESS;
- } else {
- return INPUT;
- }
- }
- /**
- * @param user the user to set
- */
- public void setUser(User user) {
- this.user = user;
- }
- /**
- * @return the user
- */
- public User getUser() {
- return user;
- }
- }
Struts2的核心配置strut.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="user" extends="struts-default" >
- <action name="loginUser" class="org.ml.action.UserAction" method="login">
- <result name="success">/welcome.jsp</result>
- <result name="input">/login.jsp</result>
- </action>
- </package>
- </struts>
还有就是web.xml的配置如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping></web-app>
上面就是本例涉及的所有文件内容,下面就看一下效果:
进行登录:
登录成功:
上面的登录登录已经成功,然我们来来看这个登录的过程:
以上是个人的一个理解,欢迎大家交流指正!
魔由心生,有万境纵横,无一道清静,无量寿佛!