Struts2环境搭建
希望能在商城项目中用上框架,因此学习了一下SSH框架,
觉得确实非常强大,现在先说一下struts2环境的搭建
1.引入库(在web-inf目录下)..
这个真心没什么好说..
2.引入web.xml文件(在web-inf目录下)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 5 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 6 <!-- 7 <display-name>Struts Blank</display-name> 8 --> 9 <filter> 10 <filter-name>struts2</filter-name> 11 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 12 </filter> 13 14 <filter-mapping> 15 <filter-name>struts2</filter-name> 16 <url-pattern>/*</url-pattern> 17 </filter-mapping> 18 19 <welcome-file-list> 20 <welcome-file>register.jsp</welcome-file> 21 </welcome-file-list> 22 23 </web-app>
register.jsp表示进入网站默认进入的网页
3.在src目录下引入struts.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE struts PUBLIC 3 "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 4 "http://struts.apache.org/dtds/struts-2.0.dtd"> 5 6 <struts> 7 <!-- 8 <constant name="struts.enable.DynamicMethodInvocation" value="false" /> 9 <constant name="struts.devMode" value="false" /> 10 11 <package name="default" namespace="/" extends="struts-default"> 12 13 <default-action-ref name="index" /> 14 15 <global-results> 16 <result name="error">/error.jsp</result> 17 </global-results> 18 19 <global-exception-mappings> 20 <exception-mapping exception="java.lang.Exception" result="error"/> 21 </global-exception-mappings> 22 23 <action name="index"> 24 <result type="redirectAction"> 25 <param name="actionName">HelloWorld</param> 26 <param name="namespace">/example</param> 27 </result> 28 </action> 29 </package> 30 31 <include file="example.xml"/> 32 --> 33 <!-- Add packages here --> 34 35 <constant name="struts.devMode" value="true" /> <!-- 开发模式 --> 36 <constant name="struts.i18n.encoding" value="gbk"/> <!-- 解决中文乱码 --> 37 <package name="register" namespace="/" extends="struts-default"> 38 39 <action name="register" class="com.shopping.action.UserAction" > 40 <result name="success"> 41 /registerOK.jsp 42 </result> 43 <result name="error"> 44 /registerFail.jsp 45 </result> 46 </action> 47 </package> 48 </struts>
register表示在jsp的form表单引用的action的名字.
另外要注意跳转的jsp前面要加"/"
4.建立一个action:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.shopping.action; 2 3 import java.sql.Timestamp; 4 5 import com.opensymphony.xwork2.ActionSupport; 6 7 public class UserAction extends ActionSupport{ 8 9 private String username; 10 private String password; 11 private String phone; 12 private String address; 13 private Timestamp rdate; 14 15 public String getUsername() { 16 return username; 17 } 18 public void setUsername(String username) { 19 this.username = username; 20 } 21 public String getPassword() { 22 return password; 23 } 24 public void setPassword(String password) { 25 this.password = password; 26 } 27 public String getPhone() { 28 return phone; 29 } 30 public void setPhone(String phone) { 31 this.phone = phone; 32 } 33 public String getAddress() { 34 return address; 35 } 36 public void setAddress(String address) { 37 this.address = address; 38 } 39 public Timestamp getRdate() { 40 return rdate; 41 } 42 public void setRdate(Timestamp rdate) { 43 this.rdate = rdate; 44 } 45 46 public String add() throws Exception { 47 // TODO Auto-generated method stub 48 System.out.println(username); 49 System.out.println(password); 50 System.out.println(phone); 51 System.out.println(address); 52 // if(name.equals("")||!name.equals("admin")){ 53 this.addFieldError("errmsg", "注册失败!"); 54 return ERROR; 55 // } 56 57 // return SUCCESS; 58 } 59 }
action里面有很多domain,这些domain的名字必须和表单中定义的各项的名字相一致,这样才能正确的得到取值
5.建立一个jsp(这里表示要跳转到struts.xml文件中action name=register所引用的action下的add方法):
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <%@ page language="java" contentType="text/html; charset=GB18030" 2 pageEncoding="GB18030"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=GB18030"> 7 <script type="text/javascript" src="scripts/regcheckdata.js"></script> 8 <title>注册界面</title> 9 </head> 10 <body> 11 12 <% 13 String path=request.getContextPath(); 14 String rpath=request.getScheme()+"://" + request.getServerName()+":" + 15 request.getServerPort() + path + "/"; 16 %> 17 18 <form action="<%=rpath %>register!add" method="post"> 19 20 <table border="2" width="90%" height="90%"> 21 <tr> 22 <td colspan="2"><center>用户注册</center></td> 23 </tr> 24 25 <tr> 26 <td>用户名:</td> 27 <td><input id="username" type="text" name="username"></td> 28 </tr> 29 30 <tr> 31 <td>密码:</td> 32 <td><input id="password" type="password" name="password"></td> 33 </tr> 34 35 <tr> 36 <td>密码确认:</td> 37 <td><input id="password2" type="password" name="password2"></td> 38 </tr> 39 40 <tr> 41 <td>电话号码:</td> 42 <td><input id="phone" type="text" name="phone"></td> 43 </tr> 44 45 <tr> 46 <td>地址:</td> 47 <td><input id="address" type="text" name="address"></td> 48 </tr> 49 50 <tr> 51 <td></td> 52 <td> 53 <input type="submit" value="注册" onclick="return checkdata()"> 54 <input type="reset" value="重置" > 55 </td> 56 </tr> 57 </table> 58 </form> 59 </body> 60 </html>