Struts2初步接觸

  使用idea快速搭建Struts2的Maven項目 html

  首先使用intelij idea部署Maven項目java

1.jar包引入 pom.xmlweb

咱們用的是阿里雲apache

我用的是最新版的Struts2核心jar包和最新版的xwork jar包服務器

只需引入這兩個節點session

 <dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.5.8</version> </dependency> <dependency> <groupId>org.apache.struts.xwork</groupId> <artifactId>xwork-core</artifactId> <version>2.3.8</version> </dependency>

 

2.接下來書寫web.xmlapp

直接上所有代碼eclipse

<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>

 

 

3.而後就是jsp頁面的書寫了(View視圖書寫),我用了兩個頁面,一個登錄頁面,一個登錄成功頁面jsp

這是登錄頁面,附加說明。我引入了javaee的jar包,否則ide

<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>
這會報錯的。還有一種解決辦法,就是在服務器引入Servletjar包
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page isELIgnored="false"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>登陸</title> <h1>登陸</h1> <s:form action="LoginAction" namespace="/" method="POST"> <s:textfield name="users.name"></s:textfield><s:fielderror fieldName="ename"></s:fielderror><br/> <s:textfield name="users.pwd"></s:textfield><s:fielderror fieldName="epwd"></s:fielderror><br/> <s:submit value="登陸"></s:submit> </s:form> <s:debug></s:debug> </head> <body> </body> </html>

這是成功頁面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page isELIgnored="false"%> <%@taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>成功</title> </head> <body> <h1>登陸成功歡迎${name}您的密碼是${pwd}</h1> </body> </html>

 

4.書寫咱們的Action

package cn.curry.entity; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import org.apache.struts2.ServletActionContext; import org.apache.struts2.interceptor.SessionAware; import javax.servlet.http.HttpSession; import java.util.Map; /** * Created by Curry on 2017/2/27. */ public class LoginAction extends ActionSupport {//還能夠實現 Action private Users users;//實體po private Map<String,Object> map; //效驗 public void validate(){ if(users.getName().length()==0){ addFieldError("ename","用戶名不爲空"); } if(users.getPwd().length()==0){ addFieldError("epwd","密碼不爲空"); } } //使用Servlet API去向Session中放入數據 public String execute() throws Exception { if (users.getName().equals("admin")&&users.getPwd().equals("1")){ //解耦 Map<String, Object> session = ActionContext.getContext().getSession(); session.put("name",users.getName()); session.put("pwd",users.getPwd()); //解耦2 /*map.put("name",users.getName()); map.put("pwd",users.getPwd());*/ //耦合 /*HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("name",users.getName()); session.setAttribute("pwd",users.getPwd());*/ return SUCCESS;//常亮 }else{ return INPUT; } } public Users getUsers() { return users; } public void setUsers(Users users) { this.users = users; } public void setSession(Map<String, Object> session) { this.map=session; } }

4.1實體類

package cn.curry.entity; /** * Created by Curry on 2017/2/27. */ public class Users { private String name; private String pwd; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } }

 

5.在classpath,寫一個名稱爲struts.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <constant name="struts.enable.DynamicMethodInvocation" value="true"/> <constant name="struts.devMode" value="true"/> <package name="default" namespace="/" extends="struts-default"> <action name="LoginAction" class="cn.curry.entity.LoginAction"> <result name="success">success.jsp</result> <result name="login">index.jsp</result> <result name="input">index.jsp</result> </action> </package> </struts>

 

6.部署運行,筆者使用的事Tomcat8,jdk事1.8

頁面效果如上圖

第一次使用Struts2來寫一個小例子,遇到問題也比較多。不少都是jar包問題,再加上idea用的不熟(以前用myeclipse),波折頗多,最後圓滿成功。

相關文章
相關標籤/搜索