投稿 评论 顶部

Springmvc 注解 伪静态(urlrewrite) 配置

佚名 网络安全


下面是web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0"?
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_3_0.xsd">
? <display-name></display-name>
? <!--配置Url Rewrite的Filter拦截所有请求 ? -->?
? <filter>?
? <filter-name>UrlRewriteFilter</filter-name>?
? ? <filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>?
? ? <init-param>?
? ? <param-name>logLevel</param-name>
? ? ? ? <param-value>WARN</param-value>?
? </init-param>
? </filter>
? <filter-mapping>?
? <filter-name>UrlRewriteFilter</filter-name>?
? <!-- *.html拦截所有html为后缀名的请求 ,/*拦截所有请求-->
? ? <url-pattern>*.html</url-pattern>?
? ? <dispatcher>REQUEST</dispatcher>?
? ? <dispatcher>FORWARD</dispatcher>?
? </filter-mapping>
? <!-- servlet 配置-->
? <servlet>
<!--springmvc的核心是DispatcherServlet,它负责控制整个页面的请求路径-->
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--初始化参数 >/WEB-INF/classes/相当于src目录-->
<init-param>
<!-- 这个param-name必须是contextConfigLocation-->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</init-param>
<!-- 初始化servlet加载的优先级 -->
<load-on-startup>1</load-on-startup>
</servlet>
<!--拦截所有以do结尾的请求-->
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!--处理从页面传递中文到后台而出现的中文乱码问题-->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
? <welcome-file-list>
? ? <welcome-file>index.jsp</welcome-file>
? </welcome-file-list>

</web-app>

********applicationContext.xml配置*******

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">


<!-- springmvc 注解驱动 -->
<mvc:annotation-driven/>
<!-- 扫描器 -->
<context:component-scan base-package="com"/>


<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>


<!-- 用于将对象转化为JSON -->
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter"/>
</list>
</property>
</bean>

</beans>

/********urlrewrite**********/

<?xml version="1.0" encoding="utf-8"?>?
<!DOCTYPE urlrewrite PUBLIC "-//tuckey.org//DTD UrlRewrite 4.0//EN"?
? ? ? ? "http://www.tuckey.org/res/dtds/urlrewrite4.0.dtd">?
<urlrewrite>?
? ? <!-- 首页 -->
? ? <rule>
? ? <from>^/index.html$</from>
? ? <to>/User/test.do?name='test'</to>
? ? </rule>
</urlrewrite>



/***********实现***********/

package com.Action;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.Pojo.User;


@Controller
@RequestMapping("/User")
public class UserAction {
@RequestMapping("/user.do")
public String getUser(){
System.out.println("我是逗比");
return "index";
}
@RequestMapping("/test.do")
public String getUser1(String name){
System.out.println("逗比是谁:"+name);
return "index";
}
@RequestMapping("/ajax.do")
public @ResponseBody User JsonUser(int userid){
System.out.println("编号:"+userid);
User user=new User();
user.setId(2);
user.setName("我是逗比");
System.out.println("学号:"+user.getId()+"姓名:"+user.getName());
?
return user;
}
}

/*****页面********/

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML>
<html>
? <head>
? ??
? ? <title>测试</title>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
? </head>
? <script type="text/javascript">
? $().ready(function(){
?
? $("#userTest").click(function(){
? var userid = $("#userid").val();
? if(userid.length == 0){
? alert("请输入编号");
? }else{
? //测试了一下,这两种方式
? /*$.post("${pageContext.request.contextPath}/User/ajax.do",{userid:userid},function(data){
alert(data.id+"------"+data.name);
},"json");*/
$.getJSON("${pageContext.request.contextPath}/User/ajax.do",{userid:userid},function(data){
alert(data.id+"------"+data.name);
});
? }
? });
? });
? </script>
? <body>
? <!-- ?<a href="${pageContext.request.contextPath}/User/user.do">逗比是小四</a><br/>
? <a href="${pageContext.request.contextPath}/User/test.do?name=小四">逗比</a>-->
? <a href="index.html">逗比是小四</a><br/>
? <input type="text" id="userid"><br/>
? <input type="button" id="userTest" value="测试">
? </body>?
</html>