首页 > 代码库 > structs环境搭建
structs环境搭建
(1)<s:fielderror />放在JSP中,如果没在web.xml中配置filter相关内容,会有The Struts dispatcher cannot be found.从而显示报错。
HTTP Status 500 - An exception occurred processing JSP page /regist.jsp at line 36
主要还是因为缺乏导入的包,对于console中的missing,not found和add这些关键字都需要注意,会提示需要导入的包。
(2)需要导入的包
(3)struts2.5.5,其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"> <display-name></display-name> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> <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> </web-app>
其中filter-class的值在struts2中配置如下,否则出错:
<filter> <filter-name>struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter>
(4)struts.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> <constant name="struts.i18n.encoding" value="gb2312"></constant> <package name="shop" namespace="/" extends="struts-default"> <action name="login" class="com.wss.action.LoginAction"> <result name="success" type="chain">show</result> <result name="error">/login.jsp</result> </action> <action name="regist" class="com.wss.action.RegistAction"> <result name="success">/login.jsp</result> <result name="error">/resist.jsp</result> </action> <action name="show" class="com.wss.action.ShowAction"> <result name="success">/suc.jsp</result> <result name="error">/error.jsp</result> </action> <action name="car" class="com.wss.action.CarAction"> <result name="success">/car.jsp</result> </action> </package> </struts>
(5)还有log4j2.xml的配置,但是我现在还不知道这个是做什么用的额。
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> </Console> </Appenders> <Loggers> <Logger name="com.demo.NamedHierarchy" level="trace"> <AppenderRef ref="Console"/> </Logger> <Logger name="com.demo" level="debug"> <AppenderRef ref="Console"/> </Logger> <Root level="error"> <AppenderRef ref="Console"/> </Root> </Loggers> </Configuration>
(6)整个项目代码的布局结构是如下:
(7)一个Action的实例:
package com.wss.action; import com.opensymphony.xwork2.ActionSupport; import com.wss.Dao.School; import com.wss.Dao.User; import com.wss.Dao.UserDao; public class RegistAction extends ActionSupport{ public RegistAction() { System.out.println("Initialization RegistAction...."); } private User user =new User(); //private User user; public User getUser() { System.out.println("Getting the getUser"); return user; } public void setUser(User user) { System.out.println("Setting the setUser"); this.user = user; } private School school; public School getSchool() { System.out.println("Getting the getSchool"); return school; } public void setSchool(School school) { System.out.println("Setting the setSchool"); this.school = school; } private String company; public void setCompany(String company) { System.out.println("Setting the company"); this.company=company; } public String execute() throws Exception{ UserDao ud =new UserDao(); System.out.println("The company is "+this.company+" The name is "+this.user.getName()+" The phone is "+this.user.getAddress()); System.out.println("The school name is "+this.school.getName()+" The city is "+this.school.getCity()+" The department is "+ this.school.getDepartment()); //if(ud.regist(user)!=0){ this.addFieldError("success", "注册成功"); return SUCCESS; //} //this.addFieldError("error", "注册失败"); //return ERROR; } }
(8)其中regist.sjp的代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%> <%@ page contentType="text/html;charset=gbk"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% 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>京东商城注册页面</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <% request.setCharacterEncoding("gbk"); %> </head> <body> <center> <form action="regist" method="post"> 用户名:<input type="text" name="user.name"/><br> 密 码:<input type="password" name="user.password"/><br> 手 机:<input type="text" name="user.phone" /><br> 地 址:<input type="text" name="user.address"/><br> 公 司: <input type="text" name="company"/> <br> 学 校:<input type="text" name="school.name"/> 城 市:<input type="text" name="school.city" /> 院 系:<input type="text" name="school.department" /> <table> <tr> <td><input type="submit" value="注册"/></td> <td><input type="reset" value="重置" ></td> </tr> </table> </form> <s:fielderror /> </center> </body> </html>
structs环境搭建
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。