最近看了一下struts,很久都没有用了,回顾了一下,strust主要的作用就是起到一个请求分发的作用,将前端不同请求交给不同的action处理,并将这些处理的结果返回给前端进行显示。这是我对strust一点简单的理解。
下面是一些简单的代码
web.xml
<?xml version="1.0" encoding="UTF-8"?> <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_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>AllTest</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置Struts2的前端控制器 --> <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>
struts.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" "http://struts.apache.org/dtds/struts-2.1.7.dtd"> <struts> <package name="login" namespace="/novel" extends="struts-default"> <!-- 拦截器 --> <interceptors> <interceptor name="firstInterceptor" class="struts.test.FirstIntceptor"></interceptor> <interceptor name="SecondInterceptor" class="struts.test.SecondIntceptor"></interceptor> <interceptor-stack name="interceptorStack"> <interceptor-ref name="firstInterceptor"></interceptor-ref> <interceptor-ref name="SecondInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <action name="login" class="struts.test.LoginAction" method="login"> <interceptor-ref name="interceptorStack"></interceptor-ref> <result name="error">/user/login.jsp</result> <result name="success">/index.jsp</result> </action> </package> </struts>
登陆页面login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK" contentType="text/html;charset=GBK"%> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>无线活动录入后台</title> <link href="http://js.soufunimg.com/wireless_m/touch/css/ztback.css" rel="stylesheet" type="text/css"/> <script src="http://js.soufunimg.com/wireless_m/touch/js/jquery.js"></script> </head> <body> <div class="wrap" style="width:330px; height:140px; margin-top:200px; border:1px solid #A2A2A2;-moz-border-radius:6px; -webkit-border-radius:6px; border-radius:6px;box-shadow: 0 0 8px #A2A2A2; -webkit-box-shadow:0 0 8px #A2A2A2; -moz-box-shadow:0 0 8px #A2A2A2; padding:20px"> <!-- action中的“/novel/login/login”,novel对应struts.xml中的package的namespace的值 login对应struts.xml中的action的name的值 --> <form class="loginForm" action="/novel/login" method="post"> <table width="100%" height="${empty param.username_change?80:120}" border="0" cellspacing="0" > <tr> <td width="13%">账号</td> <td width="87%"><input id="userName" name="userName" type="text" class="loginput" value=""/></td> </tr> <tr> <td>密码</td> <td><input id="password" name="password" type="password" class="loginput"/></td> </tr> <tr> <td></td><td id="warn" class="loginput" style="display:none;color:red">用户名或密码错误</td> </tr> </table> <input id="submit" type="submit" class="pindbarbtn ablack" style="width:60px;margin-left:auto; margin-right:auto; text-align:center; margin-top:8px; margin-bottom:8px" value="登录"></input> </form> <div> </div> <div> <label></label> </div> </div> <script type="text/javascript"> var status = "${requestScope.status}"; if(status=="error"){ $("#warn").show(); } </script> </body> </html>
业务控制器你,LoginAction.java
package struts.test; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; public class LoginAction { private String userName; 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 String checkLogin(){ HttpServletRequest request = ServletActionContext.getRequest(); if("wangbiao".equals(userName)&&"654321".equals(password)){ request.setAttribute("hello", password ); request.setAttribute("status", "success"); return "success"; }else{ request.setAttribute("status", "error"); return "error"; } } }
两个拦截器
package struts.test; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class FirstIntceptor implements Interceptor { /** * */ private static final long serialVersionUID = 1L; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } @Override public String intercept(ActionInvocation arg0) throws Exception { // TODO Auto-generated method stub System.out.println("我是第一个拦截器,action执行之前拦截"); arg0.invoke(); System.out.println("我是第一个拦截器,action执行之后拦截"); return null; } }
package struts.test; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.xml.sax.SAXException; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.Interceptor; public class SecondIntceptor implements Interceptor{ /** * */ private static final long serialVersionUID = 1L; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub Map<String, Object> paramMap = invocation.getInvocationContext().getParameters(); /** * 拦截器获取页面传过来的值 */ String[] usernameArr = (String[])paramMap.get("userName"); String[] passwordArr = (String[])paramMap.get("password"); System.out.println("我是第二个拦截器,action执行之前拦截"); System.out.println("用户名:"+usernameArr[0]+"密码:"+passwordArr[0]); if("123456".equals(passwordArr[0])){ return "success"; }else{ //就是调用相应的action invocation.invoke(); System.out.println("我是第二个拦截器,action执行之后拦截"); } return null; } }
跳转到成功的页面,index.jsp
<%@ page language="java" contentType="text/html; charset=gbk" pageEncoding="gbk"%> <!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=gbk"> <title>Insert title here</title> </head> <body> <h1>登陆成功</h1> <!-- 用el表达式获取登陆页传来的用户名和密码 --> <label>账号:${userName}</label> <label>密码:${password}</label> </body> </html>
我认为最好的老师就是实践,因为简单的看了一下struts的书,也觉得很简单,但是就是写上面的代码,也花了我一点时间。如果只是看书而不写出来运行一下的话,始终是留于表面的,而且很容易忘记。学到上面知识点最好是用代码写出来,写于求职准备中。
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-05 23:56:21