IDEA基于Maven Struts2搭建配置及示例

2018-10-11 10:01:38来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

1.web.xml加载struts框架即过滤器,要注意struts版本不同过滤器配置也不同。

 1 <!DOCTYPE web-app PUBLIC
 2         "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3         "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6   <display-name>Archetype Created Web Application</display-name>
 7 
 8   <filter>
 9     <filter-name>struts2</filter-name>
10 
11     <filter-class>
12       org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
13     </filter-class>
14 
15     <init-param>
16       <param-name>config</param-name>
17       <param-value>struts-default.xml,struts-plugin.xml,struts.xml</param-value>
18     </init-param>
19   </filter>
20 
21       <filter-mapping>
22         <filter-name>struts2</filter-name>
23         <url-pattern>/*</url-pattern>
24       </filter-mapping>
25 
26   <welcome-file-list>
27     <welcome-file>login.jsp</welcome-file>
28   </welcome-file-list>
29 
30 </web-app>
View Code

2.配置struts.xml,配置Action。(name,class,method)

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 
 3 <!DOCTYPE struts PUBLIC
 4         "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
 5         "http://struts.apache.org/dtds/struts-2.5.dtd">
 6 
 7 <struts>
 8     <package name="default" extends="struts-default" namespace="/">
 9         <action name="login" class="UserAction" method="login">
10             <result name="success">index.jsp</result>
11             <result name="login">login.jsp</result>
12         </action>
13     </package>
14 </struts>
View Code

3.View:login.jsp和index.jsp。

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2          pageEncoding="UTF-8"%>
 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=UTF-8">
 7     <title>Hello Maven</title>
 8 </head>
 9 <body>
10 <p>欢迎进入Maven Struts2应用!</p>
11 </body>
12 </html>
View Code
<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8"%>
<!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>
<form action="login" method="post">
    <table>
        <tr>
            <td>用户名:</td>
            <td><input type="text" name="username" /> </td>
        </tr>
        <tr>
            <td>密码:</td>
            <td><input type="text" name="password" /> </td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="登录" />
                <input type="reset" value="重置" /></td>
        </tr>
    </table>
</form>
</body>
</html>
View Code

4.编写Action处理类。

 1 import com.opensymphony.xwork2.ActionSupport;
 2 import org.apache.struts2.ServletActionContext;
 3 import javax.servlet.http.HttpServletRequest;
 4 import java.io.UnsupportedEncodingException;
 5 
 6 public class UserAction extends ActionSupport{
 7     @Override
 8     public String execute() throws Exception {
 9         return super.execute();
10     }
11     public String login() throws UnsupportedEncodingException {
12         HttpServletRequest request = ServletActionContext.getRequest();
13         request.setCharacterEncoding("utf-8");
14         String name = request.getParameter("username");
15         String pass = request.getParameter("password");
16         if("admin".equals(name)&&"".equals(pass)){
17             return SUCCESS;
18         }else{
19             return "login";
20         }
21     }
22 }
View Code

总结:初学struts期间遇到的struts.xml文件放置位置、struts过滤器配置、Maven依赖安装、struts流程原理等问题欢迎咨询!

转载请注明出处,谢谢!

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Hibernate 中的 idclass mapping 问题

下一篇:5.4 集合的排序(Java学习笔记)(Collections.sort(),及Arrays