Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

java - Spring Cannot find Web Address

I am trying to load this specific JSP /url. But when it executes I get the following error.

This localhost page can’t be foundNo webpage was found for the web address: http://localhost:8181/home HTTP ERROR 404

This is my Security Config

        .permitAll()
        .and().formLogin()
            // to show the page where we enter login credentials 
            .loginPage("/login") 
            // to process authentication: /login handler method implemented by Spring Security
            .loginProcessingUrl("/mylogin")
            // where to go after successful login
            .defaultSuccessUrl("/success", true)
            // to show an error page if the authentication failed
            .failureUrl("/login?Error=True")
            // everyone can access these requests
            .permitAll()

So when it is successful it goes to the url "/success". This is the following RequestMapping

@RequestMapping(value = "/success", method = RequestMethod.GET)
public String successLogin(Principal p) {
    System.out.println("Shalom56765");
    Volunteer user = volRepo.findByEmail(p.getName());
    if (user.getKind().equals(UserKind.Admin)) {
        return "/admin";
    }
    if (user.getKind().equals(UserKind.user)) {
        System.out.println("user is a user");
        return "redirect:/home";
    }

    return "login";
}}

And the mapping for "/home"

@RequestMapping("/home")
    public String login(Model model, @RequestParam String email) {
        model.addAttribute("volunteer", vrRepo.findAll());
        model.addAttribute("user_email", email);
        return "Homepage";
    }

But the error I am getting is that it can't find "/home" what am I to do is anyone seeing a simple error that I cannot see. Thanks Guys. Jeff

Main Controller

@Controller
public class MainController {
@Autowired
private VolunteerRepository volrepo;


@RequestMapping("/")
public String start() {
    return "start";
}
@RequestMapping("/sign_up")
public String signup() {
    return "sign_up";
}
@RequestMapping("/admin")
public String dashboard( Model model, BindingResult result) {
    System.out.println("IM GETTING CLICKED");
    if(result.hasErrors()) {
    }
    else {
    model.addAttribute("vols", volrepo.findAll());
    return "admin";
}
    return "/";}

}

Authentication Controller

@Controller
public class AuthenticationController {
@Autowired
private VolunteerRepository volRepo;
@Autowired
private AdminRepository adRepo;
@RequestMapping("/login")
public String login() {
    return "login";
}

@RequestMapping(value = "/success", method = RequestMethod.GET)
public String successLogin(Principal p) {
    
    Volunteer user = volRepo.findByEmail(p.getName());
    Admin user2 = adRepo.findOne(p.getName());
    System.out.println(user2.getUsername());
    if (user2.getKind().equals(UserKind.Admin)) {
        return "/admin";
    }
    if (user.getKind().equals(UserKind.user)) {
        System.out.println("user is a user");
        return "redirect:/home";
    }

    return "login";
   }}

Volunteer Controller

@Controller
public class VolunterController {
  @Autowired
  private VolunteerRepository vrRepo;
  @Autowired
  private PasswordEncoder pe;
   ArrayList<String> emails = new ArrayList<String>();
   //Add new results
    @RequestMapping(value = "/newUser",method = {RequestMethod.POST , RequestMethod.GET})
    public String newResults(@Valid @ModelAttribute Volunteer vol, BindingResult result, Model model) {
        if (result.hasErrors()) {
            
            return "sign_up";
        }
        else {
            String pass = vol.getPassword();
            String crypt = pe.encode(pass);
            vol.setPassword(crypt);
            vol.setKind(UserKind.user);
        vrRepo.save(vol);
        return "Submit";
    }}
    
    @RequestMapping("/home")
    public String login(Model model, @RequestParam String email) {
        //model.addAttribute("volunteer", vrRepo.findAll());
    //  model.addAttribute("user_email", email);
        return "Homepage";
    }
    //Add vaccine info
    @RequestMapping(value = "/newInfo", method=RequestMethod.POST)
    public String addVac(@Valid @ModelAttribute Volunteer vol, BindingResult result, Model model) {
    
            //add extra fields
        
        float dose = vol.getDose();
        String group = vol.getVac_group();
        String email = vol.getEmail();
        System.out.println(dose +" " + group + " " + email);
        List<Volunteer> vols = (List<Volunteer>) vrRepo.findAll();
            for(int i=0;i<vols.size();i++){
                String dbEmail = vols.get(i).getEmail();
                System.out.println("Checking if Email " + email + " equals " + dbEmail);
                if(dbEmail.equals(email)) {
                    System.out.println("MATCH");
                    Volunteer volUP = vrRepo.findByEmail(dbEmail);
                    volUP.setDose(dose);
                    volUP.setVac_group(group);
                    vrRepo.save(volUP);
                    System.out.println("User details are updated!");
                    break;
                }
            } 
            
        return "start";
    }
    
    //Add Positive Case
    @RequestMapping(value = "/positive/{email}", method=RequestMethod.POST)
    public String addPos(@ModelAttribute Volunteer vol, @PathVariable String email) {
    System.out.println(email);
    List<Volunteer> vols = (List<Volunteer>) vrRepo.findAll();
    System.out.println(vols.size());
    for(int i=0;i<vols.size();i++){
        String dbEmail = vols.get(i).getEmail();
        System.out.println("Checking if Email " + email + " equals " + dbEmail);
       // if(dbEmail.equals(email)) {
            System.out.println("MATCH");
            Volunteer volUP = vrRepo.findByEmail(dbEmail);
            volUP.setInfected("Positive");
            vrRepo.save(volUP);
            System.out.println("User details are updated!");
            break;
      //  }
    } 
    
        return "/";
    }
    @GetMapping(value= "/vaccine/all_result")
    public String all(Model model) {
        model.addAttribute(vrRepo.findAll());
        return "all_vaccine";
 }
    
    
    @RequestMapping(value = "/sucess", method = RequestMethod.GET)
    public String successLogin(Principal p) {
        Volunteer user = vrRepo.findByEmail(p.getName());
        if (user.getKind().equals(UserKind.user)) {
            return "redirect:/home";
        }
        else
        return "redirect:/userlogin";
    }
    @RequestMapping(value = "/login?error=true", method = RequestMethod.GET)
    public String invalidLogin(Model model) {
        model.addAttribute("error", true);
        return "userlogin";
    }
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider changing it to something like this

    @RequestMapping(value = "/home", method = RequestMethod.POST)
    public String login(@RequestBody Model model, @PathParam("email") String email) {

I'm assuming that you are you want it to be a post that takes the JSON equivalence of Model object with an optional path parameter for the email


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...