最近做springmvc注解操作,终于实验成功,可以跑动了。今天把代码贴出来,希望大家帮我继续晚上
清单一:web.xml
引用
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/*.xml</param-value>
</context-param>
<!-- 设置Spring监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- springmvc 配置 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<!-- springmvc 配置结束 -->
<!-- DWR servlet 配置结束 -->
<!-- 著名的 Character Encoding filter -->
<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>
<!--Hibernate Open Session in View Filter-->
<!-- 假设在你的应用中Hibernate是通过spring 来管理它的session.如果在你的应用中没有使用OpenSessionInViewFilter
或者OpenSessionInViewInterceptor。session会在transaction结束后关闭,此时会抛出session is close 的异常-->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
</filter>
<!-- 过滤器映射 -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 扩展spring bean的作用域有request,session,global session等-->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
</web-app>
清单2:WEB-INF/springmvc-servlet.xml
引用
<?xml version="1.0" encoding="UTF-8"?>
<!--
修改说明:
1.修改<context:component-scan以确定要加载的controller,默认为com.**.controller
2.修改class=ControllerClassNameHandlerMapping节点的pathPrefix以确定要生成的前缀
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
default-autowire="byName" >
<context:annotation-config />
<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/>
<!-- ③:对模型视图名称的解析,即在模型视图名称添加前后缀
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="view/" p:suffix=".jsp" />
-->
<!-- 注意这里新增加的dwr tag, 为使其生效,文件头中要声明namespace -->
<dwr:configuration />
<dwr:annotation-config />
<dwr:url-mapping />
<!-- 部署项目时, 请把debug设为false -->
<dwr:controller id="dwrController" debug="true" />
<!--①:规约所有进行扫描的类,以完成Bean创建和自动依赖注入的功能-->
<!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
<context:component-scan base-package="com.**.controller"/>
<!-- order值越小, 优先级越高 -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="order" value="2" />
</bean>
<!-- 文件上传-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:defaultEncoding="utf-8" />
</beans>
清单三:src/spring/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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url"
value="jdbc:mysql://localhost:3306/baoxian?useUnicode=true&amp;characterEncoding=UTF-8">
</property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean></beans>
清单四:src/spring/appapplicationContext-dao.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName" default-lazy-init="false">
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
<context:component-scan base-package="com.**.dao"/>
</beans>
清单五:src/spring/applicationContext-service.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd" default-autowire="byName" default-lazy-init="false">
<!-- component-scan自动搜索@Component , @Controller , @Service , @Repository等标注的类 -->
<!-- 默认值如果适合,该项可以不用修改 -->
<context:component-scan base-package="com.**.service" />
</beans>
清单六 SysmodelDao.java
引用
package com.baoxian.dao;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import com.baoxian.entity.Sysmodel;
@Component("sysmodelDao")
public class SysmodelDao extends HibernateDaoSupport {
public List find2(String hsql)
{
System.out.println("执行dao");
List ls = new ArrayList();
Sysmodel su = new Sysmodel();
su.setSysmodelId("1111111111");
ls.add(su);
return ls;
}
}
清单七DemoControler.java
引用
package com.baoxian.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.baoxian.dao.SysmodelDao;
@Controller
@RequestMapping("/demo/demo.htm")
public class DemoControler {
@Autowired
@Qualifier("sysmodelDao")
private SysmodelDao dao;;
@RequestMapping(params = "method=test")
public String listBoardTopic(HttpServletRequest request,HttpServletResponse response,ModelMap model) {
System.out.println("have got the login function");
String loginsuccessdir = "/demo/successful.jsp";
String logingfaulsedir = "/demo/noSuccessful.jsp";
String username = request.getParameter("username");
String pwd = request.getParameter("userpassword");
System.out.println("username="+username);
System.out.println("pwd="+pwd);
List ls =dao.find2("");
System.out.println("---------------------");
System.out.println(ls.size());
if(username==null||"".equals(username))
{
return logingfaulsedir ;
}
return loginsuccessdir;
}
}
清单 八Dwr2Controller.java
引用
package com.baoxian.controller;
import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.stereotype.Controller;
@Controller
@RemoteProxy(name="Dwr2Controller")
public class Dwr2Controller {
@RemoteMethod
public String getUserName(int id) {
System.out.println("user id is " + id);
return "UserName: " + id;
}
}
清单九 测试页面demo.html:
引用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>controller_test.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="demo.htm" method='post'>
<input type="hidden" name='method' value='test'>
<table>
<tr><td>用户名:</td><td><input type="text" name="username"/></td></tr>
<tr><td>密码:</td><td><input type="text" name="userpassword"/></td></tr>
<tr><td><input type="submit" value="确定"/></td><td><input type="reset" value="取消"/></td></tr>
</table>
</form>
</body>
</html>
清单十 测试页面webRoot/demo/dwrtest.html:
引用
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>controller_dwr.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type='text/javascript' src='../dwr/util.js'></script>
<script type='text/javascript' src='../dwr/engine.js'></script>
<script type='text/javascript' src='../dwr/interface/Dwr2Controller.js'> </script>
</head>
<script type="text/javascript">
function dwrfn()
{
Dwr2Controller.getUserName("1",callBackHello);
}
function callBackHello(data)
{
alert(data);
}
</script>
<body>
<input type ='button' value="click me to test" onclick="dwrfn()"/>
</body>
</html>
清单十一 测试结果页面webRoot/demo/successful.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 'successful.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 successful page. <br>
</body>
</html>