struts2全注解配置步骤
来源:互联网 发布:淘宝淘金币首页 编辑:程序博客网 时间:2024/05/01 06:30
第一步:
创建“struts2annotation”的web项目
第二步:
导入需要的jar包(我的环境tomcat7加jdk7)
第三步:
修改web.xml:
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <display-name>Struts Blank</display-name> <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> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list></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> 请求参数的编码方式 <constant name="struts.i18n.encoding" value="UTF-8"/> 指定被struts2处理的请求后缀类型。多个用逗号隔开 <constant name="struts.action.extension" value="action,do,go,qqi"/> 当struts.xml改动后,是否重新加载。默认值为false(生产环境下使用),开发阶段最好打开 <constant name="struts.configuration.xml.reload" value="true"/> 是否使用struts的开发模式。开发模式会有更多的调试信息。默认值为false(生产环境下使用),开发阶段最好打开 <constant name="struts.devMode" value="false"/> 设置浏览器是否缓存静态内容。默认值为true(生产环境下使用),开发阶段最好关闭 <constant name="struts.serve.static.browserCache" value="false" /> 指定由spring负责action对象的创建 <constant name="struts.objectFactory" value="spring" /> 是否开启动态方法调用 <constant name="struts.enable.DynamicMethodInvocation" value="false"/> </struts> --><struts><constant name="struts.i18n.encoding" value="UTF-8"/> <constant name="struts.action.extension" value="action,do,go,qqi"/></struts>
第五步:创建action类:
package com.ninemax.action;import org.apache.struts2.convention.annotation.Action;import org.apache.struts2.convention.annotation.ExceptionMapping;import org.apache.struts2.convention.annotation.ExceptionMappings;import org.apache.struts2.convention.annotation.Namespace;import org.apache.struts2.convention.annotation.ParentPackage;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;import com.opensymphony.xwork2.ActionSupport;/** * 使用注解来配置Action * */@ParentPackage(value = "struts-default")@Namespace(value = "/user")@Results({@Result(name = "success", location="/msg.jsp"),@Result(name="error",location="/error.jsp")})@ExceptionMappings({@ExceptionMapping(exception="java.lange.RuntimeException",result="error")})public class LoginAction extends ActionSupport {private static final long serialVersionUID = -2315464632166461L;private String loginname;private String pwd;@Action(value = "login")public String login() throws Exception{if("qq".equals(loginname) && "123".equals(pwd)){return "success";} else {return "error";}}@Action(value="add",results={@Result(name = "success",location = "/index.jsp")})public String add()throws Exception{return "success";}public String getLoginname(){return loginname;}public void setLoginname(String loginname){this.loginname = loginname;}public String getPwd(){return pwd;}public void setPwd(String pwd){this.pwd = pwd;}}
第六步:
创建对应的jsp页面
1》、创建index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2登录验证</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3>Struts2登录</h3><hr/> <form action="${pageContext.request.contextPath}/user/login.action" method="post"> <table border="1" width="500px"> <tr> <td>用户名</td> <td><input type="text" name="loginname"/></td> </tr> <tr> <td>密码</td> <td><input type="password" name="pwd"/></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"/></td> </tr> </table> </form> </body> </html>
2》、创建msg.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Struts2登录验证</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <h3>Struts2登录</h3><hr/> 登陆成功了~~ </body> </html>
3》、创建error.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%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>My JSP 'error.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--> </head> <body> This is error JSP page. <br> </body></html>
第七步:
把项目部到tomcat下启动,并且访问~!
访问的地址拼接为:http://localhost:8080/+项目名称+namespace+action名
例如我的为:http://localhost:8080/struts2annotation/user/add.action
注意在创建action类时所在的包路径名必须包含action,否则会报错。。。(其实也可以是其它几个关键字)
- struts2全注解配置步骤
- Maven 实现Struts2注解配置步骤详解
- Maven 实现Struts2注解配置步骤详解
- struts2 脱离配置文件 全注解配置
- Struts2.3.16+Spring3.2.8+Hibernate4.2.10全注解配置
- Struts2.3+Spring3.2.8+Hibernate4.1全注解配置
- Struts2.3+Spring3.2.8+Hibernate4.1全注解配置
- Struts2.3+Spring3.2.8+Hibernate4.1全注解配置
- Struts2.3+Spring3.2.8+Hibernate4.1全注解配置
- Struts2.3.16+Spring3.2.8+Hibernate4.2.10全注解配置
- SpringMVC 全注解配置
- 全注解配置事务管理
- 配置Tomcat 全步骤。
- Struts2 Annotation 注解配置
- Struts2 Annotation 注解配置
- struts2 的注解配置
- Struts2 零配置注解
- Struts2 零配置注解
- 路由器与交换机概念还是有区别滴
- 两个hadoop的问题解决
- 2012华为校园招聘机试(武汉)
- Java Date
- JQuery 跟着鼠标走的Div
- struts2全注解配置步骤
- 在VirtualBox下安装Linux的增强功能(鼠标无缝切换等)
- hdu1003.....暑期集训第一天动态规划WA了4次......
- 简单介绍设计模式
- Arcgis 导出点层
- linux命令join与paste
- asp.net中label、table、gridview不自动换行的问题
- 一 需求分析
- DateTime关于时区的学习