首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

spring3 mvc 注解,该怎么解决

2012-02-22 
spring3 mvc 注解web.xmlXML code?xml version1.0 encodingUTF-8?web-appservlet-mappingser

spring3 mvc 注解
web.xml

XML code
<?xml version="1.0" encoding="UTF-8"?><web-app>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.jpg</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.png</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.gif</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.js</url-pattern>    </servlet-mapping>    <servlet-mapping>        <servlet-name>default</servlet-name>        <url-pattern>*.css</url-pattern>    </servlet-mapping>    <servlet>        <servlet-name>spring</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>spring</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>login.jsp</welcome-file>    </welcome-file-list></web-app>

spring-servlet.xml
XML code
<?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:p="http://www.springframework.org/schema/p"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd            http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd            http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd            http://www.springframework.org/schema/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd            http://www.springframework.org/schema/mvc             http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd            http://www.springframework.org/schema/context             http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <!-- 自动扫描的包名 -->    <context:component-scan base-package="cn.spring.app.controller" />     <!-- 默认的注解映射的支持 -->    <mvc:annotation-driven />    <!-- 视图解释类 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/jsp/" />        <property name="suffix" value=".jsp" />        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />    </bean>    <!-- 拦截器 -->    <!-- 对静态资源文件的访问  -->    <mvc:default-servlet-handler /></beans>

Java code
package cn.spring.app.controller;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;@Controllerpublic class LoginController {     @RequestMapping(value="/login",method=RequestMethod.GET)    public String login(){        return "login";    }    @RequestMapping(value="/login",method=RequestMethod.POST)    public String login2(HttpServletRequest request){        String username=request.getParameter("username").trim();        System.out.println(username);        return "success";    }} 


Java code
package cn.spring.app.model;public class User {    private String userId;    private String userName;    private String userPassword;    public String getUserId() {        return userId;    }    public void setUserId(String userId) {        this.userId = userId;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getUserPassword() {        return userPassword;    }    public void setUserPassword(String userPassword) {        this.userPassword = userPassword;    }}

JScript 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>Insert title here</title></head><body>    <form action="/LoginController" method="post">        ID号: <input type="text" name="userId" id="userId"><br>        密码: <input type="password" name="userPassword" id="userPassword"><br>        <input type="submit" value="Login">    </form></body></html>

这些就是我的配置,总是报404错误,大虾们如何解决啊?

[解决办法]
虽然我没用这个做过。

但是看这里[url]http://blog.csdn.net/penngo/article/details/6038991[/url]

发现你的请求action="/LoginController"有问题。。。

lz可以根据事例程序改。。


[解决办法]
LZ你的@RequestMapping(value="/login",method=RequestMethod.POST),mapping value=里面的值与表单action="/LoginController"请求的url不一致啊,修改下就好了。
[解决办法]
探讨
我的配置有没有问题啊?为什么进入这了页面?

[解决办法]
探讨
我第一次用spring3.1mvc,以前用的是其它框架,各位大侠,会的给我改一改吧。
给一个spring3.x mvc的demo也行。一定要能跑的。不要用其它的插件生成。最好是注解的。
谢谢了。

热点排行